from django.shortcuts import HttpResponse, render, redirect
from web import models
from django.http import JsonResponse
from web.forms.task import TaskModelForm
from django.urls import reverse

def task_list(request, project_id):
    project_object = models.Project.objects.filter(id=project_id).first()
    task_list = models.DeployTask.objects.filter(project_id=project_id).all()
    return render(request, 'task_list.html', {'task_list': task_list, 'project_object': project_object})

def task_add(request, project_id):
    project_object = models.Project.objects.filter(id=project_id).first()

    if request.method == 'GET':
        form = TaskModelForm(project_object)
        return render(request, 'task_form.html', {'form': form, 'project_object': project_object})

    form = TaskModelForm(project_object, data=request.POST)
    if form.is_valid():
        form.save()
        url = reverse('task_list', kwargs={'project_id': project_id})
        return redirect(url)

    return render(request, 'task_form.html', {'form': form, 'project_object': project_object})

def hook_template(request,hook_id):
    # 根据hook_id查询出hook对象
    hook_obj = models.HookTemplate.objects.filter(pk=hook_id).first()
    back_dic = {'status':1000, 'content':''}
    back_dic['content'] = hook_obj.content
    return JsonResponse(back_dic)

def deploy(request, task_id):
    task_object = models.DeployTask.objects.filter(id=task_id).first()
    return render(request, 'deploy.html', {'task_object': task_object})




